package encaps.utils.search;

import encaps.action.BasicSearchAction;
import encaps.action.DeleteUserAction;
import encaps.action.EditUserAction;
import encaps.action.ViewUserAction;
import encaps.entities.Role;
import encaps.entities.User;
import encaps.entities.UserLocal;
import encaps.entities.UserLocalHome;
import encaps.resources.Grants;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.utils.EntityUtil;
import encaps.utils.SystemException;
import encaps.utils.XMLHelper;
import java.util.Collection;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import org.jdom.Element;

public class UserSearcher extends Searcher {

    public UserSearcher() {
        basicSearchHeaderPattern = Labels.getLabel(Labels.BASIC_USER_SEARCH_HEADER_PATTERN);
    }

    @Override
    public Element headersToXMLElement() {
        Element headers = XMLHelper.createElement(Response.COLUMN_HEADERS);

        Element loginHeader = XMLHelper.addChild(headers, Response.COLUMN_HEADER);
        XMLHelper.addChild(loginHeader, Response.COLUMN_HEADER_LABEL, Labels.getLabel(Labels.LOGIN));
        XMLHelper.addChild(loginHeader, Response.COLUMN_NAME, User.LOGIN);
        Element contactsHeader = XMLHelper.addChild(headers, Response.COLUMN_HEADER);
        XMLHelper.addChild(contactsHeader, Response.COLUMN_HEADER_LABEL, Labels.getLabel(Labels.CONTACTS));
        XMLHelper.addChild(contactsHeader, Response.COLUMN_NAME, User.CONTACTS);

        return headers;
    }

    @Override
    protected Element searchResultToTableRows(Collection users, Role role) {
        Element rows = XMLHelper.createElement(Response.ROWS);
        for (Object obj : users) {
            UserLocal user = (UserLocal) obj;
            Element row = XMLHelper.addChild(rows, Response.ROW);
            Element loginCell = XMLHelper.addChild(row, Response.CELL, user.getLogin());
            loginCell.setAttribute(Response.TYPE, Response.TEXT);

            Element contactsCell = XMLHelper.addChild(row, Response.CELL);
            contactsCell.addContent(user.getContacts());
            contactsCell.setAttribute(Response.TYPE, Response.TEXT);

            if (role.hasGrant(Grants.VIEW_USER)) {
                Element viewLinkCell = XMLHelper.addChild(row, Response.CELL);
                viewLinkCell.setAttribute(Response.TYPE, Response.LINK);
                viewLinkCell.addContent(XMLHelper.createLink(Labels.getLabel(Labels.VIEW_LINK), ViewUserAction.VIEW_USER, user.getID()));
            }
            if (role.hasGrant(Grants.EDIT_USER)) {
                Element editLinkCell = XMLHelper.addChild(row, Response.CELL);
                editLinkCell.setAttribute(Response.TYPE, Response.LINK);
                editLinkCell.addContent(XMLHelper.createLink(Labels.getLabel(Labels.EDIT_LINK), EditUserAction.EDIT_USER, user.getID()));
            }
            if (role.hasGrant(Grants.DELETE_USER)) {
                Element deleteLinkCell = XMLHelper.addChild(row, Response.CELL);
                deleteLinkCell.setAttribute(Response.TYPE, Response.LINK);
                deleteLinkCell.addContent(XMLHelper.createLink(Labels.getLabel(Labels.DELETE_LINK), DeleteUserAction.DELETE_USER, user.getID()));
            }
        }
        return rows;
    }

    @Override
    protected Collection find(SearchContext context) throws SystemException {
        try {
            UserLocalHome userHome = EntityUtil.getUserHome();

            int recordsPerPage = context.getRecordsPerPage();
            int fromRecord = context.getFromRecord();
            Collection<UserLocal> result = null;
            String searchParameter = context.getSearchParameter(BasicSearchAction.SEARCH_PARAMETER);
            result = userHome.findPaged(searchParameter, context.getOrderColumn(), context.getOrderType(), fromRecord, recordsPerPage);

            return result;
        } catch (NamingException ex) {
            throw new SystemException(Labels.getLabel(Labels.CANNOT_FIND_LOCAL_HOME_OBJECT_MESSAGE), ex);
        }

    }

    @Override
    protected int getFoundRecordsCount(SearchContext context) throws SystemException {
        try {
            UserLocalHome userHome = EntityUtil.getUserHome();
            int recordsCount = 0;
            recordsCount = userHome.getUsersCount(context.getSearchParameter(BasicSearchAction.SEARCH_PARAMETER));
            return recordsCount;
        } catch (NamingException ex) {
            throw new SystemException(Labels.getLabel(Labels.CANNOT_FIND_LOCAL_HOME_OBJECT_MESSAGE), ex);
        } catch (FinderException ex) {
            //это не должно произойти
            throw new SystemException(ex);
        }
    }
}
